<template>
  <div style="background-color: #FFFFFF; padding: 20px;">
    <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
      <el-form-item label="项目名称" prop="name">
        <el-input v-model="ruleForm.name"></el-input>
      </el-form-item>

      <el-form-item label="项目key" prop="project_key">
        <el-input v-model="ruleForm.project_key"></el-input>
      </el-form-item>

      <el-form-item label="项目描述" prop="describe">
        <el-input type="textarea" v-model="ruleForm.describe"></el-input>
      </el-form-item>

      <el-form-item label="数据库" prop="database">
        <el-select
          style="width: 100%;"
          v-model="optionsDatabaseId"
          multiple
          filterable
          remote
          reserve-keyword
          placeholder="请输入数据库名称"
          :remote-method="getDatabaseInfo"
          :loading="loading">
          <el-option
            v-for="(item,index) in database_info"
            :key="index"
            :label="item.db_name"
            :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="环境" prop="environment">
        <el-select
          style="width: 100%;"
          v-model="optionsEnvironmentId"
          multiple
          filterable
          remote
          reserve-keyword
          placeholder="请输入环境名称"
          :remote-method="getEnvironmentInfo"
          :loading="loading">
          <el-option
            v-for="(item,index) in environment_info"
            :key="index"
            :label="item.env_title"
            :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>

      <!--</el-form-item>-->
      <el-form-item label="项目状态" prop="status">
        <el-select v-model="ruleForm.status" placeholder="请选择状态" style="width: 100%;">
          <el-option
            v-for="(item,index) in optionsCommon"
            :key="index"
            :label="item.value"
            :value="item.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm()">保存</el-button>
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>

</template>
<script>
  export default {
    name: 'Addpjt',
    data() {
      return {
        // 通用配置
        optionsCommon: [],  // 存储CommonChoices实际数据的列表
        CHOICES: {},  // 项目所有的CHOICES
        // EditData: {},  // 将要编辑的所有数据
        EditUrl: this.$http.projectURL,  // get编辑的URL
        EditIdStr: 'project_id',  // 模块id字符串
        pushObjName: 'project',

        loading: false,
        database_info: [], // 数据列表，获取1000条项目数据
        environment_info: [], // 环境列表，获取1000条项目数据

        optionsDatabaseId: [],
        optionsEnvironmentId: [],

        ruleForm: {
          name: '',
          project_key: '',
          describe: '',
          database: '',//数据库连接表
          environment: '',//IP端口协议表
          status: '',
          database_info: {}, // 数据
          environment_info: {}, // 环境
        },
        rules: {
          name: [
            {required: true, message: '请输入项目名称', trigger: 'blur'},
            {min: 3, max: 50, message: '长度在 3 到 50 个字符', trigger: 'blur'}
          ],
          describe: [
            {required: true, message: '请输入项目描述', trigger: 'blur'},
            {min: 0, max: 1000, message: '长度在 0 到 1000 个字符', trigger: 'blur'}
          ],
          database: [
            {required: true, message: '请输入数据库连接表', trigger: 'blur'}
          ],
          environment: [
            {required: true, message: '请输入IP端口协议表', trigger: 'blur'},
          ],
          status: [
            {required: true, message: '请选择状态', trigger: 'change'}
          ],
        },
      };
    },

    created() {
      this.getEditData();   // get回将要编辑的数据
      this.optionsCommon = this.getChoices.common  // 获取标准CHOICES
    },

    methods: {
      submitForm() {
        this.$myTool.submitForm(this)
      },

      // get回将要编辑的数据
      getEditData() {
        this.$myTool.getEditData(this)
      },

      // get将要编辑的数据
      resetForm(formName) {
        this.$refs[formName].resetFields();
      },

      // 数据库信息搜索
      getDatabaseInfo(query) {
        this.$myTool.getObjList(this, this.$http.databaseURL, this.database_info, query)
      },
      // 环境信息
      getEnvironmentInfo(query) {
        this.$myTool.getObjList(this, this.$http.environmentURL, this.environment_info, query)
      },
    },
    watch: {
      'ruleForm.environment_info': function (value) {
        if (value) {
          this.optionsEnvironmentId.push(value.id);
          this.environment_info.push(value);
        } else {
          this.optionsEnvironmentId = [];
          this.environment_info = [];
        }
      },
      'ruleForm.database_info': function (value) {
        if (value) {
          this.optionsDatabaseId.push(value.id);
          this.database_info.push(value);
        } else {
          this.optionsDatabaseId = [];
          this.database_info = [];
        }
      },
      optionsDatabaseId: function (value) {
        value = this.$myTool.delLenghtList(1, value, this);
        this.ruleForm.database = value[0];
      },
      optionsEnvironmentId: function (value) {
        value = this.$myTool.delLenghtList(1, value, this);
        this.ruleForm.environment = value[0]
      },
    },
    // 计算属性
    computed: {
      getChoices() {
        return this.$store.state.choicesInfo
      }
    },
  }
</script>
